<style type="text/css">
        .layui-table-cell {
            height: auto;
            line-height: 28px;
        }
    </style>
<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">Banner维护</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!home_console">首页</a>
          <a><cite>Banner维护</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">状态：</label>
					<div class="layui-input-block">
				      <select name="state">
				        <option value="0">显示</option>
				        <option value="1">隐藏</option>
				      </select>
				    </div>
				</div>

				<div class="layui-inline">
					<button id="user-btn-search"
						class="layui-btn layuiadmin-btn-useradmin" lay-submit
						lay-filter="LAY-front-search">
						<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
					</button>
					<button id="btn-reset" class="layui-btn icon-btn">
						<i class="layui-icon">&#x1006;</i>重置
					</button>
					<!--<button id="product-btn-add" class="layui-btn icon-btn" ><i class="layui-icon">&#xe654;</i>添加-->
				</div>
			</div>

		</div>

        <table class="layui-table" id="banner-table" lay-filter="banner-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="banner-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <!--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
</script>

<script type="text/html" id="homeShow-tpl-state">
	{{# if (d.state === 0) { }}  
		<button class="layui-btn layui-btn-xs layui-btn-warm">展示</button>
	{{# } else if(d.state === 1) { }}  
		<button class="layui-btn layui-btn-xs layui-btn-danger">隐藏</button>
	{{# } }}
</script>

<script>
    layui.use(['form', 'table', 'util', 'admin', 'element'], function () {
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var element = layui.element;

        form.render('select');

        // 渲染表格
        table.render({
            elem: '#banner-table',
            url: 'system/banner/list',
            page: true,
            cols: [[
                {type: 'numbers'},
                {field: 'picPath', title: '展示图片',templet: function (d) {
                    return "<img src='"+d.picPath+"' style='height:66px;width:100px;'>";
                }},
                {field: 'url', title: '跳转地址'},
                {field: 'describe', title: '描述'},
                {field: 'state', templet: '#homeShow-tpl-state',align:'center', title: '是否展示'},
                {
                    sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {align: 'center', toolbar: '#banner-table-bar', title: '操作',minWidth:'150'}
            ]]
        });

        // 添加按钮点击事件
        $('#product-btn-add').click(function () {
            showEditModel();
        });

        // 工具条点击事件
        table.on('tool(banner-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') { // 删除
				layer.confirm('确定要删除吗？', function() {
					layer.load(2);
					admin.req("system/banner/delete", {
						id : obj.data.id
					}, function(data) {
						layer.closeAll('loading');
						if (data.code == 1) {
							layer.msg(data.msg, {
								icon : 1
							});
							obj.del();
						} else {
							layer.msg(data.msg, {
								icon : 2
							});
						}
					}, "post");
				});
            }
        });

        // 显示表单弹窗
        var showEditModel = function (data) {
            var title = data ? '修改Banner' : '新增Banner';
            admin.putTempData('t_banner', data);
            admin.popupCenter({
            	area: ['600px', '400px'],
            	offset: '10%',
            	height:"50%",
                title: title,
                fixed: false,
                path: 'system/banner/editForm',
                finish: function () {
                    table.reload('banner-table', {});
                }
            });
        };

      //监听搜索
        form.on('submit(LAY-front-search)', function(data){
          var field = data.field;
          table.reload('banner-table', {where: {searchKey: "state",searchValue:field.state}});
        });

        
     // 搜索按钮点击事件
		$('#btn-reset').click(function() {
			location.reload();
		});
    });
</script>